package com.hxz.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hxz.entity.*;
import com.hxz.admin.service.AdminIndexService;
import com.hxz.admin.service.impl.AdminIndexServiceImpl;
import com.hxz.interceptor.LoginInterceptor;

import io.swagger.annotations.Api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 后台管理系统主页面
 * @author hxz
 * @description TODO
 * @data 2019年12月5日 上午9:31:28
 */
@Controller
@RequestMapping("/admin")
@Api(description="后台管理系统主页面controller")
public class AdminIndexController {
	@Autowired
	private LoginInterceptor loginInterceptor;
	@Autowired
	private AdminIndexService adminIndexService;

	/**
	 * 登录后后台系统主页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws Exception {
		if (loginInterceptor.preHandle(request, response, session)) {
			List<Article> articles = adminIndexService.getArticleList();
			List<ArticleComment> articleComments = adminIndexService.getCommentsList();
			List<Article> categories = adminIndexService.getCategoryList();
			Integer articleNum=adminIndexService.getArticleNum();
			Integer commentNum=adminIndexService.getCommentNum();
			request.setAttribute("articles", articleNum);
			request.setAttribute("articleList", articles);
			request.setAttribute("articleComments", commentNum);
			request.setAttribute("articleCommentList", articleComments);
			request.setAttribute("categories", categories.size());
			request.setAttribute("categoryList", categories);
			return "admin/index";
		} else {
			return "admin/login";
		}

	}
}
